package com.woniu.his.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ShiroConfig {

    //配置过滤器
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition defaultShiroFilterChainDefinition = new DefaultShiroFilterChainDefinition();
        defaultShiroFilterChainDefinition.addPathDefinition("/login", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/register", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/static/**", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/logout", "logout");
        defaultShiroFilterChainDefinition.addPathDefinition("/**", "user");
        return defaultShiroFilterChainDefinition;
    }

//    @Bean
//    public Realm realm(CredentialsMatcher credentialsMatcher) {
//        MyRealm myRealm = new MyRealm();
//        //添加验证规则
//        myRealm.setCredentialsMatcher(credentialsMatcher);
//        return myRealm;
//    }

    @Bean
    public Realm shiroRealm(){
        return new MyRealm();
    }

    //放入shiro的加密Bean
//    @Bean
//    public CredentialsMatcher credentialsMatcher() {
//        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//        hashedCredentialsMatcher.setHashAlgorithmName(EntryptRule.HASH_NAME);
//        hashedCredentialsMatcher.setHashIterations(EntryptRule.HASH_ITERATION);
//        return hashedCredentialsMatcher;
//    }

    //支持thymeleaf的方言
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
}
